// pages/myLike/myLike.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    postList: [],
    hasBindForDel: false,
    postid: 0,
    likes: 0,

    //刷新本页数据
    hasChange: false
  },

  to_p3: function () {
    //刷新p2
    app.p2_shuaxin=true
    // wx.switchTab({
    //   url: '../p3/p3'
    // })
    wx.navigateBack({
      delta: 1
    })
    //防止重复赋值给postList
    app.myLikeList = [],
      //刷新广场页
      app.hasAddPost = true
  },

  kantie_myPost: function (e) {
    // wx.redirectTo({
    //   url: '../../kantie/kantie',
    // })

    //防止重复赋值给postList
    app.myLikeList = []
    var ccc = e.currentTarget.dataset.id; //获取view中的药用currentTarget
    app.postid = ccc;
    app.back_myPost = 3;
    //console.log(ccc);
    wx.navigateTo({
      url: '../p1_into/p1_into',
    })
  },

  not_delPost:function(){
    this.setData({
      hasBindForDel: false
    })
  },
  deletePost: function (e) {
    var ccc = e.currentTarget.dataset.id; //获取view中的药用currentTarget
    var ddd = e.currentTarget.dataset.likes;
    this.setData({
      hasBindForDel: true,
      postid: ccc,
      likes: ddd - 1
    })
  },
  item1: function () {
    this.setData({
      hasBindForDel: false
    })
  },
  item2: function (e) {
    this.setData({
      hasBindForDel: false
    });

    
    app.myLikeList = [],

    //取消收藏,即取消点赞
    wx.request({
      url: 'http://120.24.97.106:8081/selectPostLike',
      method: 'POST',
      data: {
        "postid": this.data.postid,
        "likeuserid": app.userid,
      },
      success: (res1) => {
        if (res1.data.length != 0) {
          wx.request({
            url: 'http://120.24.97.106:8081/delPostLike',
            method: 'POST',
            data: {
              "postid": this.data.postid,
              "likeuserid": app.userid,
            },
            success: (res2) => {
              console.log("取消收藏")

              //防止重复赋值给postList
              app.myLikeList = [],

              wx.showLoading({
                title: '取消收藏..',
              })
              setTimeout(function () {
                wx.redirectTo({
                  url: '../myLike/myLike'
                })
                wx.hideLoading()
              }, 500)

            }
          });
          wx.request({
            url: 'http://120.24.97.106:8081/updateLikes',
            method: 'POST',
            data: {
              "id": this.data.postid,
              "likes": this.data.likes
            }
          });
        }
      }
    });


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.request({
      url: 'http://120.24.97.106:8081/getPostIdByLike',
      method: 'POST',
      data: {
        "likeuserid": app.userid,
      },
      success: (res1) => {
        // console.log(res1.data)
        for (var i = 0; i < res1.data.length; i++) {
          // console.log( res1.data[i].postid)
          wx.request({
            url: 'http://120.24.97.106:8081/findMyLike',
            method: 'POST',
            data: {
              "id": res1.data[i].postid,
            },
            success: (res2) => {
              // console.log(res2.data[0])
              app.myLikeList.push(res2.data[0])
              // console.log(app.myLikeList)
              this.setData({
                postList: app.myLikeList
              })
            }
          })
        }
      }
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.myLikeList = []
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})